<template>
    <s-layout title="城市列表">
        <view class="city-select-container">
            <!-- 搜索栏 -->
            <view class="city-search-bar">
                <input class="city-search-input" placeholder="输入城市名或拼音查询" v-model="searchQuery" />
            </view>
            <!-- 当前/历史选择城市 -->
            <view class="city-section city-history">
                <view class="city-section-title">当前选择/历史选择城市</view>
                <view class="city-history-list">
                    <view v-for="city in historyCities" :key="city" class="city-history-item" @tap="selectCity(city)">{{
                        city }}</view>
                </view>
            </view>

            <!-- 城市列表 -->
            <scroll-view scroll-y class="city-list-scroll" :scroll-into-view="scrollToLetter"
                :scroll-with-animation="true" ref="cityScrollView">
                <view v-for="(group, letter) in filteredCityList" :key="letter" :id="'city-letter-' + letter">
                    <view class="city-letter">{{ letter }}</view>
                    <view v-for="city in group" :key="city" class="city-list-item" @tap="selectCity(city)">{{ city }}
                    </view>
                </view>
            </scroll-view>
            <!-- 右侧字母索引 -->
            <view class="city-index-bar">
                <view v-for="letter in cityIndex" :key="letter" class="city-index-item" @tap="scrollTo(letter)">{{
                    letter }}
                </view>
            </view>
        </view>
    </s-layout>
</template>

<script setup>
import { ref, computed } from 'vue';
import sheep from "@/sheep";
import { onLoad } from "@dcloudio/uni-app";

const searchQuery = ref('');
const historyCities = ref(['北京', '上海']);


const allCities = ref({
    A: ['安庆市', '安阳市', '鞍山市', '安康市'],
    B: ['保定市', '包头市', '巴塞罗那', '北京市', '波士顿', '蚌埠市', '宝鸡市', '白银市'],
    C: ['长春市', '长沙市', '长治市', '常州市', '成都市', '赤峰市', '重庆市', '沧州市', '常德市'],
    D: ['大连市', '大理市', '大庆市', '东莞市', '德阳市', '大同市', '东营市'],
    E: ['鄂尔多斯市', '恩施市', '鄂州市'],
    F: ['福州市', '佛山市', '抚顺市', '阜阳市', '防城港市'],
    G: ['广州市', '贵阳市', '桂林市', '赣州市', '广元市', '广安市'],
    H: ['杭州市', '合肥市', '哈尔滨市', '海口市', '呼和浩特市', '邯郸市', '衡阳市', '黄石市'],
    J: ['济南市', '嘉兴市', '九江市', '吉林市', '金华市', '晋城市', '荆州市'],
    K: ['昆明市', '开封市', '克拉玛依市', '喀什市', '昆山市'],
    L: ['兰州市', '洛阳市', '拉萨市', '乐山市', '丽江市', '连云港市', '辽阳市'],
    M: ['马鞍山市', '茂名市', '眉山市', '梅州市', '绵阳市'],
    N: ['南京市', '宁波市', '南昌市', '南宁市', '南阳市', '南通市', '宁德市'],
    P: ['平顶山市', '莆田市', '盘锦市', '攀枝花市', '濮阳市'],
    Q: ['青岛市', '泉州市', '秦皇岛市', '齐齐哈尔市', '曲靖市'],
    R: ['日照市', '瑞安市'],
    S: ['上海市', '深圳市', '苏州市', '沈阳市', '石家庄市', '三亚市', '绍兴市', '宿迁市'],
    T: ['天津市', '太原市', '唐山市', '泰安市', '台州市', '泰州市'],
    W: ['武汉市', '温州市', '无锡市', '乌鲁木齐市', '威海市', '渭南市', '芜湖市'],
    X: ['西安市', '厦门市', '徐州市', '湘潭市', '西宁市', '襄阳市', '新乡市'],
    Y: ['银川市', '扬州市', '烟台市', '宜昌市', '岳阳市', '运城市', '宜宾市'],
    Z: ['郑州市', '珠海市', '淄博市', '张家口市', '镇江市', '中山市', '自贡市']
});

const cityIndex = computed(() => Object.keys(allCities.value));

const filteredCityList = computed(() => {
    if (!searchQuery.value) return allCities.value;
    const result = {};
    for (const [letter, cities] of Object.entries(allCities.value)) {
        const filtered = cities.filter(city => city.includes(searchQuery.value));
        if (filtered.length) result[letter] = filtered;
    }
    return result;
});

const scrollToLetter = ref('');
const cityScrollView = ref(null);

function selectCity(city) {
    // 这里可以做页面跳转、emit事件、存储等
    uni.showToast({ title: `已选择：${city}`, icon: 'none' });
}

function scrollTo(letter) {
    scrollToLetter.value = 'city-letter-' + letter;
    // 兼容性处理：部分平台可能需要延迟清空 scrollToLetter
    setTimeout(() => {
        scrollToLetter.value = '';
    }, 500);
}
const goBack = () => {
    uni.navigateBack()
};
</script>

<style lang="scss">
.city-select-container {
    background: #ffffff;
    min-height: 100vh;
    position: relative;
    font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
}

.city-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 32rpx 24rpx 16rpx 24rpx;
    background: #fff;
    font-size: 36rpx;
    font-weight: 600;
    border-bottom: 1px solid #f0f0f0;
    position: sticky;
    top: 0;
    z-index: 10;
}

.back-icon {
    font-size: 36rpx;
    color: #222;
}

.city-title {
    flex: 1;
    text-align: center;
    color: #222;
}

.city-header-actions {
    display: flex;
    gap: 16rpx;
}

.city-header-btn {
    font-size: 28rpx;
    color: #888;
    background: #f3f4f6;
    border-radius: 50%;
    width: 48rpx;
    height: 48rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}

.city-search-bar {
    background: #ffffff;
    padding: 24rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.city-search-input {
    width: 100%;
    height: 80rpx;
    border-radius: 40rpx;
    border: none;
    background: #fff;
    font-size: 30rpx;
    padding: 0 32rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;

    &:focus {
        box-shadow: 0 4rpx 16rpx rgba(59, 130, 246, 0.2);
    }
}

.city-section {
    margin: 24rpx 0 0 0;
    padding: 0 24rpx;
}

.city-section-title {
    font-size: 26rpx;
    color: #888;
    margin-bottom: 12rpx;
}

.city-hot-list {
    display: flex;
    flex-wrap: wrap;
    gap: 18rpx 24rpx;
}

.city-hot-item {
    background: #fff;
    border: 1px solid #E53979;
    color: #E53979;
    border-radius: 20rpx;
    padding: 20rpx 48rpx;
    font-size: 30rpx;
    font-weight: 500;
    margin-bottom: 12rpx;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    cursor: pointer;
    box-shadow: 0 2rpx 8rpx rgba(59, 130, 246, 0.1);

    &:active {
        background: #2563eb;
        color: #fff;
        box-shadow: 0 4rpx 12rpx rgba(59, 130, 246, 0.2);
        transform: translateY(1rpx);
    }
}

.city-history-list {
    display: flex;
    gap: 16rpx;
}

.city-history-item {
    background: #f5f5f5;
    color: #E53935;
    border-radius: 12rpx;
    padding: 10rpx 28rpx;
    font-size: 26rpx;
}

.city-list-scroll {
    max-height: calc(100vh - 400rpx);
    overflow-y: auto;
    margin: 0 24rpx;
    background: #fff;
    border-radius: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.03);
    padding: 12rpx 0;
}

.city-letter {
    font-size: 28rpx;
    color: #E53935;
    font-weight: 600;
    padding: 16rpx 0 8rpx 24rpx;
    background: #f7f8fa;
}

.city-list-item {
    font-size: 28rpx;
    color: #222;
    padding: 18rpx 24rpx;
    border-bottom: 1px solid #f0f0f0;
    background: #fff;
    transition: background 0.2s;
    cursor: pointer;

    &:active {
        background: #e0e7ef;
    }
}

.city-index-bar {
    position: fixed;
    right: 24rpx;
    top: 240rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 20;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 24rpx;
    padding: 12rpx 4rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.city-index-item {
    font-size: 24rpx;
    color: #E53935;
    padding: 6rpx 0;
    width: 36rpx;
    text-align: center;
    border-radius: 10rpx;
    transition: all 0.3s ease;
    font-weight: 500;
    margin: 2rpx 0;

    &:active {
        background: #E53935;
        color: #fff;
        transform: scale(1.1);
    }
}
</style>